import java.io.*;
import java.net.*;
import java.awt.*;
import java.util.*;

import javax.swing.*;

public class ServerClient {

	JTextArea display;
	ArrayList clientOutputStreams;
	String name;
	int numOfUsers = 0;
	ArrayList<Integer> ids = new ArrayList<Integer>();
	ArrayList<String> whoIsOnlineA = new ArrayList<String>();
	ChatClient CC = new ChatClient();
	
	public static void main(String[] args){
		new ServerClient().go();
	}

	public void go(){
		JFrame theFrame = new JFrame("Server for Rat");
		JPanel thePanel = new JPanel();
		Box theBox = new Box(BoxLayout.X_AXIS);
		thePanel.setLayout(new BoxLayout(thePanel, BoxLayout.Y_AXIS));
		display = new JTextArea(15,35);
		display.setWrapStyleWord(true);
		display.setLineWrap(true);
		display.setEditable(false);
		
		JScrollPane scroller = new JScrollPane(display);
		scroller.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
		scroller.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		
		thePanel.add(BorderLayout.CENTER, scroller);
		
		
		theFrame.setResizable(false);
		theFrame.setDefaultCloseOperation(theFrame.EXIT_ON_CLOSE);
		theFrame.setSize(300,300);
		theFrame.add(BorderLayout.CENTER, thePanel);
		theFrame.pack();
		theFrame.setVisible(true);
		setUpForGo();
		
	}

	
	public void setUpForGo(){
		clientOutputStreams = new ArrayList();
		BufferedReader reader2;
		
		try{
			ServerSocket serverSock = new ServerSocket(5000);
			while(true){
				Socket clientSocket = serverSock.accept();

				PrintWriter writer = new PrintWriter(clientSocket.getOutputStream());
				clientOutputStreams.add(writer);
				InputStreamReader stream2 = new InputStreamReader(clientSocket.getInputStream());
				reader2 = new BufferedReader(stream2);
				Thread t = new Thread(new ClientHandler(clientSocket));
				t.start();
				name = reader2.readLine();
				display.append(name +" connected" + "\n");
				tellEveryone("Server: " + name + " joined");
				whenUserJoins();
				
				}
		} catch (Exception ex) {
			//
			ex.printStackTrace();
		}
		
	}
	
	private void whenUserJoins(){
		ids.add(numOfUsers);
		numOfUsers++;
		whoIsOnlineA.add(name);

		
		

	}
	
	private void whoIsOnline(){
		display.append("Number of users in chat: " + numOfUsers + "\n");
		tellEveryone("Server: " + "Number of users in chat: " + numOfUsers);
		display.append("Users in chat: " + numOfUsers + "\n");
		tellEveryone("Server: " + "Users in chat: ");
		for (int i = 0; i < whoIsOnlineA.size();i++){
			display.append(whoIsOnlineA.get(i) + ", ");
			tellEveryone(whoIsOnlineA.get(i) + ", ");
		}
		display.append("ID's: " + numOfUsers + "\n");
		tellEveryone("Server: " + "ID's: ");
		for (int i = 0; i < ids.size();i++){
			display.append(ids.get(i) + ", ");
			tellEveryone(ids.get(i) + ", ");
		}
	}
	
	private void tellEveryone(String msg){
		Iterator it = clientOutputStreams.iterator();
		while(it.hasNext()){
			try{
				PrintWriter writer = (PrintWriter) it.next();
				writer.println(msg);
				writer.flush();
			}catch(Exception ex){ex.printStackTrace();}
		}
	}

	public class ClientHandler implements Runnable {
		BufferedReader reader;
		Socket sock;
		
		public ClientHandler(Socket clientSocket){
			try{
				
				sock = clientSocket;
				InputStreamReader stream = new InputStreamReader(sock.getInputStream());
				reader = new BufferedReader(stream);

			}catch(Exception ex){ex.printStackTrace(); /* hue */}
		}
		
		@Override
		public void run() {
				String message;
				try {
					while((message = reader.readLine()) != null){
						if (message.equals("/users")){
							tellEveryone("whoIsOnline ne raboti //");
							whoIsOnline();
						}else{
						display.append(message + "\n");
						tellEveryone(message);
						}
						
					}
				} catch (IOException ex) {
					//display.append("There was a error in ClientHandler");
					
				}
			
		}//end of run
		
		
	}
	
	
	
	}












